package com.teaphy.coroutinecrawler.flow.operators.exception

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.retry
import java.lang.IndexOutOfBoundsException

fun main() = runBlocking {
    flow {
        var num = 0
        while (true) {
            emit(num)
            num++
            if (num > 3) {
                throw IndexOutOfBoundsException("不能发出大于3的数")
            }
            delay(100)
        }
    }.retry(2) {
        if (it is IndexOutOfBoundsException) {
            println("exception: $it")
            true
        } else {
            false
        }
    }.collect {
        println("receive : $it")
    }
}